package element
{
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
    import flash.utils.*;
	
	import data.Map;
	
	public class Paopao extends Elem
	{
		private var _paopao:MovieClip;
        private var _scope:int = 1; // bomb scope
        private var _ct:Timer; // create timer
        private var _lt:Timer; // live timer
		private var _area:Array = [];

        public var _bombPaopao:Function;
        public var _bombPerson:Function;
        public var _bombElement:Function;
        public var _displayPaopao:Function;
        public var _removePaopao:Function;
        public var _owner:Object;

		public function Paopao(mc:MovieClip, row:int, col:int)
		{
			_paopao = mc;
			_area = new Array();

			super(mc, row, col);

            _ct = new Timer(500, 1);
			_lt = new Timer(3000, 1);
			
            _ct.start();
            _ct.addEventListener(TimerEvent.TIMER_COMPLETE, onCreateTimerComplete);
		}

        private function onCreateTimerComplete(e:TimerEvent):void
        {
            if (_displayPaopao != null) {
                _displayPaopao(this, this._owner);
				_paopao.gotoAndStop("jumpping");
				_lt.start();
				_lt.addEventListener(TimerEvent.TIMER_COMPLETE, onLiveTimerComplete);
				collectAreas();
            }
        }
		
        public function bomb():void
        {
            _ct.stop();
            _lt.stop();
            collectAreas();
            onLiveTimerComplete(null);
        }

		private function onLiveTimerComplete(e:TimerEvent):void
		{
            var x:Object;

			_paopao.gotoAndStop("bomb");
            if (_bombPerson != null) {
                _bombPerson(this.row, this.col);
            }

            if (_removePaopao != null) {
                _removePaopao(this);
            }

            while ((x = _area.pop()) != undefined) {
                trace("bomb:", x.r, x.c, Map.finderName(Map.data[x.r][x.c]));
                if (_bombPerson != null) {
                    _bombPerson(x.r, x.c);
                }
                if (Map.data[x.r][x.c] == Map.E_Floor) {
                    if (_bombPaopao != null) {
                        _bombPaopao(x.r, x.c, this._owner);
                    }
                } else if (Map.data[x.r][x.c] == Map.E_BoxHinder
                        || Map.data[x.r][x.c] == Map.E_GrassHinder) {
                    if (_bombPaopao != null) {
                        _bombPaopao(x.r, x.c, this._owner);
                    }
                    if (_bombElement != null) {
                        setTimeout(_bombElement, 500, x.r, x.c);
                    }
                }
            }
		}

		private function collectAreas():void
		{
			if (this._owner != null) {
				this._scope = 1;
				if (this._owner._doubleEnergy) {
					this._scope = 2;
				}
				
                switch (this._scope) {
                    case 1:
                        if (this.row - 1 >= 0) {
                            if (Map.finderBombed(Map.data[this.row - 1][this.col])) {
                                _area.push({r:this.row - 1, c:this.col});
                            }
                        }
                        if (this.row + 1 <= Map.row - 1) {
                            if (Map.finderBombed(Map.data[this.row + 1][this.col])) {
                                _area.push({r:this.row + 1, c:this.col});
                            }
                        }
                        if (this.col - 1 >= 0) {
                            if (Map.finderBombed(Map.data[this.row][this.col - 1])) {
                                _area.push({r:this.row, c:this.col - 1});
                            }
                        }
                        if (this.col + 1 <= Map.col - 1) {
                            if (Map.finderBombed(Map.data[this.row][this.col + 1])) {
                                _area.push({r:this.row, c:this.col + 1});
                            }
                        }
                        break;
                    case 2:
                        if (this.row - 2 >= 0) {
                            if (Map.finderBombed(Map.data[this.row - 2][this.col])) {
                                _area.push({r:this.row - 2, c:this.col});
                            }
                        }
                        if (this.row - 1 >= 0) {
                            if (Map.finderBombed(Map.data[this.row - 1][this.col])) {
                                _area.push({r:this.row - 1, c:this.col});
                            }
                        }
                        if (this.row + 1 <= Map.row - 1) {
                            if (Map.finderBombed(Map.data[this.row + 1][this.col])) {
                                _area.push({r:this.row + 1, c:this.col});
                            }
                        }
                        if (this.row + 2 <= Map.row - 1) {
                            if (Map.finderBombed(Map.data[this.row + 2][this.col])) {
                                _area.push({r:this.row + 2, c:this.col});
                            }
                        }
                        if (this.col - 2 >= 0) {
                            if (Map.finderBombed(Map.data[this.row][this.col - 2])) {
                                _area.push({r:this.row, c:this.col - 2});
                            }
                        }
                        if (this.col - 1 >= 0) {
                            if (Map.finderBombed(Map.data[this.row][this.col - 1])) {
                                _area.push({r:this.row, c:this.col - 1});
                            }
                        }
                        if (this.col + 1 <= Map.col - 1) {
                            if (Map.finderBombed(Map.data[this.row][this.col + 1])) {
                                _area.push({r:this.row, c:this.col + 1});
                            }
                        }
                        if (this.col + 2 <= Map.col - 1) {
                            if (Map.finderBombed(Map.data[this.row][this.col + 2])) {
                                _area.push({r:this.row, c:this.col + 2});
                            }
                        }
                        break;
                    default:
                        break;
				}
			}
		}
	}
}
